import QtQuick 1.0
import Qt.labs.folderlistmodel 1.0

Dialog {
    id: fileChooser

    property string currentImage: defaultImageFile

    property string baseDir
    property variant sets: ["","resourceid_524981", "resourceid_524982", "resourceid_524983"]
    property int set: 0

    property bool firstFile: true

    function getFolder() {
        var folder = ""
        if (fileChooser.set == 0) {
            folder = thumbnailDataRoot;
        } else {
            folder = thumbnailDataRoot+"drm/data/"+fileChooser.sets[fileChooser.set];
        }

        return folder;
    }

    Column {
        id: setColumn
        anchors {
            top: parent.top
            bottom: parent.bottom
            left: parent.left
        }

        width: 120

        SetSelector {
            set: 0
            width: parent.width
            onClicked: fileChooser.set = 0
        }
        SetSelector {
            set: 1
            width: parent.width
            onClicked: fileChooser.set = 1
        }
        SetSelector {
            set: 2
            width: parent.width
            onClicked: fileChooser.set = 2
        }
        SetSelector {
            set: 3
            width: parent.width
            onClicked: fileChooser.set = 3
        }
    }

    GridView {
        id: previewGrid
        FolderListModel {
            id: folderModel
            folder: getFolder()
            nameFilters: ["*.png"]
            showDirs: false
            showDotAndDotDot: false
        }

        interactive: false
        model: folderModel
        anchors.top: parent.top
        anchors.left: setColumn.right
        anchors.leftMargin: 10
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        cellWidth: 160
        cellHeight: 110

        delegate: Thumbnail {
            // Sometimes FolderListModel gets confused and includes also folders in the
            // model even if we have set showDirs to false. If that happens, set the
            // delegate's size to 0,0
            width: folderModel.isFolder(index) ? 0 : previewGrid.cellWidth
            height: folderModel.isFolder(index) ? 0 : previewGrid.cellHeight
            source: folderModel.isFolder(index) ? "" : "image://thumbnail/"+sets[set]+"/"+fileName
            onClicked: {
                fileChooser.currentImage = sets[set]+"/"+fileName
                fileChooser.hide()
            }
        }
    }

    Button {
        sourceImage: "back"
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        onClicked: fileChooser.hide()
    }
}
